package com.hgq.jssjyyxt.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgq.jssjyyxt.mapper.ForumPostsMapper;
import com.hgq.jssjyyxt.entity.ForumPosts;
import com.hgq.jssjyyxt.service.ForumPostsService;
import org.springframework.stereotype.Service;
import com.hgq.jssjyyxt.util.Result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (ForumPosts)表服务实现类
 *
 * @author makejava
 * @since 2024-04-11 13:53:42
 */
@Service("forumPostsService")
public class ForumPostsServiceImpl extends ServiceImpl<ForumPostsMapper, ForumPosts> implements ForumPostsService {


    @Override
    public Result selectAll(ForumPosts forumPosts, Integer limit, Integer page,String userName) {
        List<Map<String, Object>> list = baseMapper.selectAll(forumPosts, (page - 1) * limit, limit,userName);

        //获取查询的所有id
        List<Integer> ids = baseMapper.selectAllIds(forumPosts, (page - 1) * limit, limit,userName);
        if (!ids.isEmpty()){
            //查询出所有子数据
            List<Map<String, Object>> allComments = baseMapper.selectAllComments(ids,userName);
            for (Map<String, Object> map : list) {
                List<Map<String, Object>> objectObjectHashMap = new ArrayList<>();
                //遍历allComments,如果存在id和map的id相等，则添加到map的children中
                for (Map<String, Object> comment : allComments) {
                    if (comment.get("postId").equals(map.get("postId"))) {
                        objectObjectHashMap.add(comment);
                    }
                }
                map.put("children", objectObjectHashMap);
            }
        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("count", baseMapper.selectAllCount(forumPosts,userName));
        result.put("data", list);
        return Result.success(result);
    }
}

